
package keter.service;

import keter.model.Menu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.tx.Tx;

public class MenuService{
    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(MenuService.class);
    
    //jFinal特效对象    
    public static MenuService service = Enhancer.enhance(MenuService.class);

    /**
     * 保存菜单，同时更新父节点的isleaf属性
     * @param menu
     * @return 
     * @author  gulixing@msn.com
     * @date    2015年12月10日
     */
    @Before(Tx.class)
    public boolean save(Menu menu) {
        Menu pmenu = Menu.dao.findById(menu.getID("parent_id"));
        //保存子菜单
        if(pmenu!=null){
	        //更新父节点的isleaf标识
	        pmenu.set("isleaf", "false").update();
	        //设置root_id为父菜单的root_id
	        menu.set("root_id",pmenu.get("root_id"));
	        return menu.save();
        }
        
        //保存顶层菜单
        menu.save();
        menu.set("root_id",menu.getID());//更新root_id为自身id
    	return menu.update();
    }
}
